package org.papervision3d.core.proto
{
    import org.papervision3d.core.geom.*;
    import org.papervision3d.objects.DisplayObject3D;

    public class FindNearestCallback extends FindCallback
    {
        public var x:Number;
        public var y:Number;

        public var distance:Number;

        public function FindNearestCallback(x:Number, y:Number, mindistance:Number = Infinity)
        {   
            this.x = x;
            this.y = y;
            this.distance = mindistance;
        }

        public override function face(face:Face3D, object:Mesh3D):void
        {
			if( ! object.enabled ) return;
        	var triangle : DrawTriangle = face.drawtriangle;
            if (distance == 0)
                if (tri != null)
                    if (triangle.screenZ > tri.screenZ)
                        return;

            if (triangle.inside(x, y))
            {
                distance = 0;
                found(triangle, face, object);
                return;
            }
            if (distance == 0) return;

            var newdist:Number = triangle.distanceToCenter(x, y);
            if (newdist < distance)
            {
                distance = newdist;
                found(triangle, face, object);
            }
        }

    }
}
